from selenium import webdriver
import time,os
from utils.log import logger
from common.report_generate import Report


class Login():
    def __init__(self):
        # 打开浏览器
        self.wd = webdriver.Chrome()
        # 打开登录主页面
        self.wd.get("http://192.168.1.146:8080/woniuboss3.5/login")
        self.wd.maximize_window()
        self.wd.implicitly_wait(30)

    #读取文件
    def read_casedata_txt(self):
        casefile = "D:/python/WoniuBoss/data/logindata.txt"
        with open(file=casefile, mode='r', encoding='utf-8') as f:
            line = f.readline()
            listcase = []
            listcase.append(line.strip('\n'))
            while line:
                line = f.readline()
                if line == '':
                    continue
                else:
                    listcase.append(line.strip('\n'))
        return listcase

    #登录
    def do_login(self,username,password):
        self.wd.find_element_by_name("userName").clear()
        self.wd.find_element_by_name("userName").send_keys(username)
        self.wd.find_element_by_name("userPass").clear()
        self.wd.find_element_by_name("userPass").send_keys(password)
        self.wd.find_element_by_name("checkcode").clear()
        self.wd.find_element_by_name("checkcode").send_keys("0000")
        self.wd.find_element_by_class_name('modal-footer').click()
        # 停顿几秒
        time.sleep(5)
        # 验证登录
        insert = Report()
        try:
            self.wd.find_element_by_link_text("[注销]").click()
            print("登录成功")
            insert.write_report('3.5','登录模块','GUI测试','TC-001','正确的用户名密码登录','成功','无','无')
        except:
            print("登录失败")
            img = insert.pic_screenshot()
            insert.write_report('3.5', '登录模块', 'GUI测试', 'TC-001', '错误的用户名密码登录', '失败', '无', img)

if __name__ == '__main__':
    class1 = Login()
    casedata = class1.read_casedata_txt()
    for dataline in casedata:
        username,password = dataline.split(',')
        class1.do_login(username,password)

